package com.mall.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
public class RedisUtil {
    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;

    /*
    默认过期时长，单位：秒
     */
    public static final long DEFAULT_EXPIRE = 60 * 60 * 24;

    /*
    不设置过期时长
     */
    public static final long NOT_EXPIRE = -1;

    /*
    *判断key是否存在
    @param[key]
    *@return boolean
    *@author ojj
    *@date 2020/4/18 13:14
    */
    public boolean existsKey(String key) {
        return redisTemplate.hasKey(key);
    }

    /*
    *重命名key，如果newkey已经存在，则newkey的原值被覆盖
    @param[oldKey, newKey]
    *@return void
    *@author ojj
    *@date 2020/4/18 13:14
    */
    public void renameKey(String oldKey, String newKey) {
        redisTemplate.rename(oldKey, newKey);
    }

    /*
    *newkey不存在时才重命名
    @param[oldKey, newKey]
    *@return void
    *@author ojj
    *@date 2020/4/18 13:14
    */
    public void renameKeyNotExist(String oldKey, String newKey) {
        redisTemplate.renameIfAbsent(oldKey, newKey);
    }

    /*
    *删除key
    @param[key]
    *@return void
    *@author ojj
    *@date 2020/4/18 13:13
    */
    public void deleteKey(String key) {
        redisTemplate.delete(key);
    }

    /*
    *删除多个key
    @param[keys]
    *@return void
    *@author ojj
    *@date 2020/4/18 13:13
    */
    public void deleteKeys(String... keys) {
        Set<String> kSet = Stream.of(keys).map(k -> k).collect(Collectors.toSet());
        redisTemplate.delete(kSet);
    }

    /*
    *删除key的集合
    @param[keys]
    *@return void
    *@author ojj
    *@date 2020/4/18 13:13
    */
    public void deleteKeys(Collection<String> keys) {
        Set<String> kSet = keys.stream().map(k -> k).collect(Collectors.toSet());
    }

    /*
    *设置key的生命周期
    @param[key, time, timeUnit]
    *@return void
    *@author ojj
    *@date 2020/4/18 13:12
    */
    public void expireKey(String key, long time, TimeUnit timeUnit) {
        redisTemplate.expire(key, time, timeUnit);
    }

    /*
    *指定key的过期日期
    @param[key, date]
    *@return void
    *@author ojj
    *@date 2020/4/18 13:12
    */
    public void expireKeyAt(String key, Date date) {
        redisTemplate.expireAt(key, date);
    }

    /*
    *查询key的生命周期
    @param[key, timeUnit]
    *@return long
    *@author ojj
    *@date 2020/4/18 13:11
    */
    public long getKeyExpire(String key, TimeUnit timeUnit) {
        return redisTemplate.getExpire(key, timeUnit);
    }

    /*
    *将key设置为永久有效
    @param[key]
    *@return void
    *@author ojj
    *@date 2020/4/18 13:12
    */
    public void persistKey(String key) {
        redisTemplate.persist(key);
    }
}